﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web.Services;
using System.Xml;
using System.Xml.Serialization;
using App.Codes;
using Common;


namespace SHCMPService
{
    /// <summary>
    /// SHCMPService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]

    public class SHCMPService : System.Web.Services.WebService
    {

        [WebMethod]
        public string Login(string name, string password)
        {
            string personid =
                new ClsCon().GetFieldValue("SELECT Id AS personcount FROM dbo.SysPerson WHERE Name='" + name +
                                           "' AND Password='" + EncryptAndDecrypte.EncryptString(password) + "'");
            if (!string.IsNullOrEmpty(personid))
            {

                return "成功"+personid;
            }
            else
            {
                return "失败";
            }
        }

        [WebMethod]
        public string GetBusiness(string userguid)
        {

            DataTable dtBusiness =
                new ClsCon().CreatDaTable("SELECT RowGuid,BusinessName FROM View_PersonAndBusiness WHERE Id='" +
                                          userguid + "'");

            dtBusiness.TableName = "Business";
            return SerializeDataTableXml(dtBusiness);
        }
        [WebMethod]
        public string GetOrderCount(string userguid)
        {


            return new ClsCon().GetFieldValue("SELECT COUNT(1) AS ordercount FROM dbo.[Order] WHERE CreatePerson='" + userguid + "' AND ISNULL(IsWork,0)=0");
        }
        [WebMethod]
        public string InsertOrder(string userguid, string BusinessGuid,string type)
        {
            bool isinsert =
                   new ClsCon().Insert(
                       "INSERT INTO dbo.[Order]( RowGuid ,CreatePerson ,CreateTime,BusinessGuid ,Type)VALUES  ( NEWID() ,'" + userguid + "' ,GETDATE() ,'" + BusinessGuid + "' ,'" + type + "')");
            if (isinsert)
                return "1";
            else
            {
                return "0";
            }

        }

        private string SerializeDataTableXml(DataTable pDt)
        {
            // 序列化DataTable
            StringBuilder sb = new StringBuilder();
            XmlWriter writer = XmlWriter.Create(sb);
            XmlSerializer serializer = new XmlSerializer(typeof (DataTable));
            serializer.Serialize(writer, pDt);
            writer.Close();

            return sb.ToString();
        }

    }
}
